<?php

/**
 * TiposRecursos actions.
 *
 * @package    SistemaWebUnlSenescyt
 * @subpackage TiposRecursos
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ReportesActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $idproyecto = $request->getParameter('id_pro');
        $this->getUser()->setAttribute('id_prore', $idproyecto);
        $q = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro=?', $idproyecto);
        $this->proyecto = $q->fetchOne();
    }

    public function executeCronogramas(sfWebRequest $request) {
        $aux = $this->getUser()->getAttribute('id_prore');
        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $aux);
        $this->proyecto = $pro->fetchOne();
        $fq1 = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_pro = ?', $aux)
                ->andWhere('f.defecto = 1');
        $this->fdefecto = $fq1->fetchOne();

        $fq2 = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_pro = ?', $aux)
                ->andWhere('f.defecto = 0');
        $this->fcontraparte = $fq2->fetchOne();
    }

    public function executeCronogramaFondo2(sfWebRequest $request) {
       $this->pro = $this->getUser()->getAttribute('id_prore');
        $idfo = $request->getParameter('id_fo');
        $this->getUser()->setAttribute('id_fo', $idfo);

        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idfo);
        $this->fondo = $f->fetchOne();
        $this->getUser()->setAttribute('id_fo', $idfo);

        $p = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro = ?', $this->pro);
        $this->proyecto = $p->fetchOne();


        $this->objetivos = Doctrine_Core::getTable('objetivo')->createQuery('o')
                ->where('o.id_pro = ?', $this->pro)
                ->execute();
        
         $this->techos = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $this->pro)
                ->execute();
         
         $this->descripciones = Doctrine_Core::getTable('descripciongeneral')->createQuery('d')
                ->where('d.id_pro = ?', $this->pro)
                ->execute();
    }

    public function executeCronogramaFondo2L(sfWebRequest $request) {
        $this->pro = $this->getUser()->getAttribute('id_prore');
        $idfo = $request->getParameter('id_fo');
        $this->getUser()->setAttribute('id_fo', $idfo);

        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idfo);
        $this->fondo = $f->fetchOne();
        $this->getUser()->setAttribute('id_fo', $idfo);

        $p = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro = ?', $this->pro);
        $this->proyecto = $p->fetchOne();


        $this->objetivos = Doctrine_Core::getTable('objetivo')->createQuery('o')
                ->where('o.id_pro = ?', $this->pro)
                ->execute();
        
         $this->techos = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $this->pro)
                ->execute();
         
         $this->descripciones = Doctrine_Core::getTable('descripciongeneral')->createQuery('d')
                ->where('d.id_pro = ?', $this->pro)
                ->execute();
    }

    public function executeCronogramaFondo(sfWebRequest $request) {
        $this->pro = $this->getUser()->getAttribute('id_prore');
        $idfo = $request->getParameter('id_fo');
        $this->getUser()->setAttribute('id_fo', $idfo);

        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idfo);
        $this->fondo = $f->fetchOne();
        $this->getUser()->setAttribute('id_fo', $idfo);

        $p = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro = ?', $this->pro);
        $this->proyecto = $p->fetchOne();


        $this->objetivos = Doctrine_Core::getTable('objetivo')->createQuery('o')
                ->where('o.id_pro = ?', $this->pro)
                ->execute();
        
         $this->techos = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $this->pro)
                ->execute();
         
         $this->descripciones = Doctrine_Core::getTable('descripciongeneral')->createQuery('d')
                ->where('d.id_pro = ?', $this->pro)
                ->execute();
    }

    public function executePresupuestos(sfWebRequest $request) {
        $aux = $this->getUser()->getAttribute('id_prore');
        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $aux);
        $this->proyecto = $pro->fetchOne();
        $fq1 = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_pro = ?', $aux)
                ->andWhere('f.defecto = 1');
        $this->fdefecto = $fq1->fetchOne();

        $fq2 = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_pro = ?', $aux)
                ->andWhere('f.defecto = 0');
        $this->fcontraparte = $fq2->fetchOne();
    }

    public function executePresupuestoFondo2(sfWebRequest $request) {
        $this->pro = $this->getUser()->getAttribute('id_prore');
        $idfo = $request->getParameter('id_fo');
        $this->getUser()->setAttribute('id_fo', $idfo);
        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idfo);
        $this->fondo = $f->fetchOne();
        $f2 = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro = ?', $this->pro);
        $this->proyecto = $f2->fetchOne();
        
        
        $this->getUser()->setAttribute('id_fo', $idfo);
        $this->techospresupuestarioss = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $this->fondo->getIdPro())
                ->execute();
    }

    public function executePresupuestoFondo2L(sfWebRequest $request) {
         $this->pro = $this->getUser()->getAttribute('id_prore');
        $idfo = $request->getParameter('id_fo');
        $this->getUser()->setAttribute('id_fo', $idfo);
        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idfo);
        $this->fondo = $f->fetchOne();
        $f2 = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro = ?', $this->pro);
        $this->proyecto = $f2->fetchOne();
        
        
        $this->getUser()->setAttribute('id_fo', $idfo);
        $this->techospresupuestarioss = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $this->fondo->getIdPro())
                ->execute();
    }

    public function executePresupuestoFondo(sfWebRequest $request) {
        $this->pro = $this->getUser()->getAttribute('id_prore');
        $idfo = $request->getParameter('id_fo');
        $this->getUser()->setAttribute('id_fo', $idfo);
        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idfo);
        $this->fondo = $f->fetchOne();
        $f2 = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro = ?', $this->pro);
        $this->proyecto = $f2->fetchOne();
        
        
        $this->getUser()->setAttribute('id_fo', $idfo);
        $this->techospresupuestarioss = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $this->fondo->getIdPro())
                ->execute();
    }

    public function executeResumenes(sfWebRequest $request) {
        $aux = $this->getUser()->getAttribute('id_prore');
        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $aux);
        $this->proyecto = $pro->fetchOne();
        
    }

  public function executeResumenFondo2(sfWebRequest $request) {
        $aux = $request->getParameter('id_pro');
        $this->idpro = $aux;
        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $aux);
        $this->proyecto = $pro->fetchOne();
        $matrisfechainicial = 0;
        $matrisfechainicial = explode("-", $this->proyecto->getPeriodos()->getFechai());
        $matrisfechafinal = 0;
        $matrisfechafinal = explode("-", $this->proyecto->getPeriodos()->getFechaf());
        $this->añoinicial = $matrisfechainicial[0];        
        $this->añofinal = $matrisfechafinal[0];
        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $request->getParameter('id_fo'));                
        $this->fondo = $f->fetchOne();      
        
        $this->techos = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $aux)
                ->execute();
    }

    public function executeResumenFondo2L(sfWebRequest $request) {
        $aux = $request->getParameter('id_pro');
        $this->idpro = $aux;
        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $aux);
        $this->proyecto = $pro->fetchOne();
        $matrisfechainicial = 0;
        $matrisfechainicial = explode("-", $this->proyecto->getPeriodos()->getFechai());
        $matrisfechafinal = 0;
        $matrisfechafinal = explode("-", $this->proyecto->getPeriodos()->getFechaf());
        $this->añoinicial = $matrisfechainicial[0];        
        $this->añofinal = $matrisfechafinal[0];
        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $request->getParameter('id_fo'));                
        $this->fondo = $f->fetchOne();      
        
        $this->techos = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $aux)
                ->execute();
    }

    public function executeResumenFondo(sfWebRequest $request) {
        //$aux = $this->getUser()->getAttribute('id_prore');
        
        $aux = $request->getParameter('id_pro');
        $this->idpro = $aux;
        $this->pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $aux);
        $this->proyecto = $this->pro->fetchOne();
       
        $matrisfechainicial = 0;
        $matrisfechainicial = explode("-", $this->proyecto->getPeriodos()->getFechai());
        $matrisfechafinal = 0;
        $matrisfechafinal = explode("-", $this->proyecto->getPeriodos()->getFechaf());
        $this->añoinicial = $matrisfechainicial[0];        
        $this->añofinal = $matrisfechafinal[0];
        $this->f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $request->getParameter('id_fo'));                
        $this->fondo = $this->f->fetchOne();      
        
        $this->techos = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $aux)
                ->execute();
       
    }

    public function executeResumenFondoPDF(sfWebRequest $request) {

        $aux = $request->getParameter('id_pro');
        $idpro = $aux;
        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $aux);
        
        $proyecto = $pro->fetchOne();
        
              
        $matrisfechainicial = 0;
        $matrisfechainicial = explode("-",$proyecto->getPeriodos()->getFechai());
        $matrisfechafinal = 0;
        $matrisfechafinal = explode("-", $proyecto->getPeriodos()->getFechaf());
        $añoinicial = $matrisfechainicial[0];        
        $añofinal = $matrisfechafinal[0];
        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $request->getParameter('id_fo'));                
        $fondo = $f->fetchOne();      
        
        $techos = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $aux)
                ->execute();
       
        $config = sfTCPDFPluginConfigHandler::loadConfig();
        $pdf = new sfTCPDF("L", "mm", "A4", true, 'UTF-8', false);
        $pdf->SetFont('FreeSerif', '', 12);
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', '13'));
        $pdf->SetHeaderData(PDF_HEADER_LOGO, 60, 'SIWEBSU', 'Sistema Web de Gestión de Presupuestos SENESCYT-UNL');
        $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        $pdf->AliasNbPages();
        $pdf->AddPage();
        
            $pdf->SetFillColor(3,10,20,6); 
            $pdf->SetDrawColor(37, 51, 100, 17);
            $pdf->SetTextColor(255, 255, 255,255);
        $pdf->ln();
        
        $pdf->writeHTML('<div align="center"><b>PROYECTO: </b>'.' '.$proyecto->getNombre().'</div>');
        
        $pdf->Ln(4);  
        
        $pdf->writeHTML('<div align="center"><b>RESUMEN PRESUPUESTARIO ENTIDAD FINANCIERA'.' '.strtoupper($fondo->getSiglasInst()).'</b></div>');
        $pdf->Ln(4);
        
        $pdf->cell(70,10,"Rubro",1,0,"C");
        
        for ($i = $añoinicial; $i <= $añofinal; $i++) {
          $pdf->cell(30,10,$i,1,0,"C");  
        }
        
        $pdf->cell(30,10,"Total",1,1,"C");
        
        foreach ($techos as $t):
            
            $total1 = 0;
        
            $pdf->cell(70,10,$t->getNombre(),1,0,"C");
          
            for ($i = $añoinicial; $i <= $añofinal; $i++) {
                            $descripciones = Doctrine_Core::getTable('descripciongeneral')->createQuery('d')
                                    ->where('d.id_tp = ?', $t->getIdTp())
                                    ->execute();
                            $sumavalor1 = 0;
                            foreach ($descripciones as $de):
                                $valores = Doctrine_Core::getTable('valor')->createQuery('v')
                                        ->where('v.id_dg = ?', $de->getIdDg())
                                        ->andwhere('v.id_fo = ?', $fondo->getIdFo())
                                        ->andwhere('v.anio = ?', $i)
                                        ->execute();
                                foreach ($valores as $va):
                                    $sumavalor1 = $sumavalor1 + $va->getVal();
                                endforeach;
                            endforeach;
            
            $pdf->cell(30,10,round($sumavalor1, 2),1,0,"C");
            $total1 = $total1 + $sumavalor1;
            }
            $pdf->cell(30,10,round($total1, 2),1,1,"C");
            
        endforeach;
           $pdf->cell(70,10,"TOTAL",1,0,"C");
            $totalhorizontal1 = 0;
                    for ($i = $añoinicial; $i <= $añofinal; $i++) {
                        
                        $valores = Doctrine_Core::getTable('valor')->createQuery('v')
                                ->where('v.id_fo = ?', $fondo->getIdFo())
                                ->andwhere('v.anio = ?', $i)
                                ->execute();
                        $sumavalorvertical1 = 0;
                        
                        foreach ($valores as $va):
                            $sumavalorvertical1 = $sumavalorvertical1 + $va->getVal();
                        endforeach;
                        
                         $pdf->cell(30,10,$sumavalorvertical1,1,0,"C");
                         $totalhorizontal1 = $totalhorizontal1 + $sumavalorvertical1;
                         
                       } 
                         $pdf->cell(30,10,$totalhorizontal1,1,0,"C"); 
                        
        $pdf->Output('Resumen_Presupuestario_'.strtoupper($fondo->getSiglasInst()).'.pdf', 'I');
        echo "<a href=doc.pdf>Descargar doc.pdf</a>";
        throw new sfStopException();
    }

         
    public function executeAnalisisFondo(sfWebRequest $request) {
        $aux = $this->getUser()->getAttribute('id_prore');
        $this->idpro = $aux;
        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $aux);
        $this->proyecto = $pro->fetchOne();
        $fo = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_pro = ?', $aux);
        $this->fond = $fo->fetchOne();
        $lista = Doctrine::getTable('valor')->createQuery('v')
                ->where('v.id_fo = ?', $this->fond->getIdFo())
                ->execute();
        
        
        
        $this->presupuesto = 0;
        foreach ($lista as $val):
            $this->presupuesto = $this->presupuesto + $val->getVal();
        endforeach;
        $this->si = false;
        if ($this->presupuesto > 0) {
            $this->si = true;   
        }
        
        
    }
    public function executeAnalisis(sfWebRequest $request) {

        $aux = $this->getUser()->getAttribute('id_prore');
        $this->idpro = $aux;
        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $aux);
        $this->proyecto = $pro->fetchOne();
        $fq1 = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_pro = ?', $aux)
                ->andWhere('f.defecto = 1');
        $this->fdefecto = $fq1->fetchOne();
        $fq2 = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_pro = ?', $aux)
                ->andWhere('f.defecto = 0');
        $this->fcontraparte = $fq2->fetchOne();
        $listacontraparte = Doctrine::getTable('valor')->createQuery('v')
                ->where('v.id_fo = ?', $this->fcontraparte->getIdFo())
                ->execute();
        $listadefecto = Doctrine::getTable('valor')->createQuery('v')
                ->where('v.id_fo = ?', $this->fdefecto->getIdFo())
                ->execute();
        
        
        
        $this->presupuestocontraparte = 0;
        foreach ($listacontraparte as $val1):
            $this->presupuestocontraparte = $this->presupuestocontraparte + $val1->getVal();
        endforeach;

        $this->presupuestodefecto = 0;
        foreach ($listadefecto as $val2) {
            $this->presupuestodefecto = $this->presupuestodefecto + $val2->getVal();
        }        
        
        $this->si = false;
        if ($this->presupuestocontraparte > 0 && $this->presupuestodefecto > 0) {
            $this->si = true;          
            
        }
    }

    public function executeExportarAnalisisPDF(sfWebRequest $request) {

       $this->executeAnalisisFondo($request);

        $this->executeAnalisis($request);
        
        $auxs = $request->getParameter('id_pro');
        $idpro = $auxs;
        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $auxs);      

        $proyectos= $pro->fetchOne();
        
        $config = sfTCPDFPluginConfigHandler::loadConfig();

        $pdf = new sfTCPDF("L", "mm", "A4", true, 'UTF-8', false);

        
        
        $pdf->SetFont('FreeSerif', '', 8);

        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', '10'));
        $pdf->SetHeaderData(PDF_HEADER_LOGO, 60, 'SIWEBSU', 'Sistema Web de Gestión de Presupuestos SENESCYT-UNL');
        $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
              
            
            
        $pdf->AliasNbPages();
        $pdf->AddPage();
        if ($this->si == true) {
            
            $pdf->writeHTML('<br><h3>PROYECTO:'.' '.$proyectos->getNombre().'</h3>');
            
            $pdf->writeHTML('<div align="center"><h2><b>ANÁLISIS PRESUPUESTARIO</b></h2></div>');
            
            $pdf->SetFillColor(3,10,20,6); 
            $pdf->SetDrawColor(84, 15, 100, 3);
            $pdf->SetTextColor(255, 255, 255,255);
            
            
            $pdf->cell(90, 10, 'RUBROS', 1, 0, "C");
            $pdf->cell(40, 10, 'Valores' . '  ' . strtoupper($this->fond->getSiglasInst()), 1, 0, "C");
            $pdf->cell(60, 10, '% FINANCIAMIENTO' . '   ' . strtoupper($this->fond->getSiglasInst()), 1, 0, "C");
            $pdf->cell(30,10,'VALORES'.'   '.strtoupper($this->fcontraparte->getSiglasInst()),1,0,"C");
            $pdf->cell(40,10,'% FINANCIAMIENTO'.'  '.strtoupper($this->fcontraparte->getSiglasInst()),1,1,"C");
             
            $porcentajetotal1 = 0;
                $porcentajetotal2 = 0;
                $seguimiento = 0;
                $totalsenescyt = 0;
                
                $techos = Doctrine_Core::getTable('techos')->createQuery('t')
                        ->where('t.id_pro = ?', $this->idpro)
                        ->execute();
                foreach ($techos as $tp):
                    $idauxtp = $tp->getIdTp();
                    $sumavalor1 = 0;
                    $sumavalor2 = 0;
                    $descripciones = Doctrine_Core::getTable('descripciongeneral')->createQuery('d')
                            ->where('d.id_tp = ?', $tp->getIdTp())
                            ->execute();


                    foreach ($descripciones as $de):
                        $vals = Doctrine_Core::getTable('valor')->createQuery('v')
                                ->where('v.id_dg = ?', $de->getIdDg())
                                ->execute();

                        foreach ($vals as $va):
                            if ($va->getIdFo() == $this->fdefecto->getIdFo()) {
                                $sumavalor1 = $sumavalor1 + $va->getVal();
                            } else {
                                if ($va->getIdFo() == $this->fcontraparte->getIdFo()) {
                                    $sumavalor2 = $sumavalor2 + $va->getVal();
                                }
                            }
                        endforeach;

                    endforeach;


                    $porcentaje1 = 0;
                    $porcentaje1 = (100 * $sumavalor1) / $this->presupuestodefecto;
                    $porcentajetotal1 = $porcentajetotal1 + $porcentaje1;
                    $porcentaje2 = 0;
                    $porcentaje2 = (100 * $sumavalor2) / $this->presupuestocontraparte;
                    $porcentajetotal2 = $porcentajetotal2 + $porcentaje2;
                    
                    $pdf->cell(90, 10, $tp->getNombre(), 1, 0, "C", 0, '', 3);
                    $pdf->cell(40, 10, round($sumavalor1,2), 1, 0, "C");
                    $pdf->cell(60, 10, round($porcentaje1,2), 1, 0, "C");
                    $pdf->cell(30, 10, round($sumavalor2,2), 1, 0, "C");
                    $pdf->cell(40, 10, round($porcentaje2,2), 1, 1, "C");
                  
                    endforeach;
                    
                    $pdf->cell(90, 10, 'MONTO DEL APORTE'.' '.strtoupper($this->fdefecto->getSiglasInst()), 1, 0, "C");
                    $pdf->cell(40, 10, round($this->presupuestodefecto,2), 1, 0, "C");
                    $pdf->cell(60, 10,'' , 1, 0, "C");
                    $pdf->cell(30, 10, round($this->presupuestocontraparte,2), 1, 0, "C");
                    $pdf->cell(40, 10, round($this->porcentaje2,2).' '.'%', 1, 1, "C");
                    
                    $pdf->cell(90, 10, '% DE SEGUIMIENTO Y  MONITOREO  (  5% )', 1, 0, "C");
                    $pdf->cell(40, 10, round($seguimiento = $this->presupuestodefecto * 0.05,2), 1, 0, "C");
                    $pdf->cell(60, 10,$seg=(($seguimiento/$this->presupuestodefecto)*100).''.'%' , 1, 1, "C");
                    
                    $pdf->cell(90, 10, 'FINANCIAMIENTO'.' '.strtoupper($this->fdefecto->getSiglasInst()), 1, 0, "C");
                    $pdf->cell(40, 10, round($totalsenescyt = $seguimiento + $this->presupuestodefecto,2), 1, 1, "C");
                    
                   
                    $pdf->cell(90, 10, 'APORTE DE LA ENTIDAD PRINCIPAL EJECUTORA (CONTRAPARTE  MÍNIMO)', 1, 0, "C", 0, '', 3);
                    $pdf->cell(40, 10, round($this->presupuestocontraparte,2), 1, 1, "C");
                    
                    $pdf->cell(90, 10, 'MONTO TOTAL DE EJECUCIÓN DEL PROYECTO', 1, 0, "C");
                    $pdf->cell(40, 10, round($this->presupuestocontraparte+$seguimiento + $this->presupuestodefecto,2), 1, 0, "C");
                    $pdf->cell(60, 10,round($porcentajetotal1 + $seg,2) , 1, 0, "C");
                    $pdf->cell(30, 10, round($totalsenescyt+$this->presupuestocontraparte,2), 1, 0, "C");
                    $pdf->cell(40, 10, 'TOTAL PROYECTO', 1, 1, "C");
                    
              }else{
                  $pdf->writeHTML('<br><b>No se puede realizar el analisis presupuestario porque no existen valores ingresados en los correspondientes fondos</b></br>');
              }         
                    
             $proys=utf8_decode($proyectos->getNombre());       
                    
        $pdf->ln();
        $pdf->Output('AnalisisPresupuestario_PROYECTO:_'.$proys.'.pdf', 'I');

        throw new sfStopException();
    }
    
    public function executeExportarAnalisisPDF2(sfWebRequest $request) {

       $this->executeAnalisisFondo($request);

       $auxs = $request->getParameter('id_pro');
        $idpro = $auxs;
        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('.id_pro = ?', $auxs);      

        $proyectoss= $pro->fetchOne();
        
        $config = sfTCPDFPluginConfigHandler::loadConfig();

        $pdf = new sfTCPDF("L", "mm", "A4", true, 'UTF-8', false);

        $pdf->SetFont('FreeSerif', '', 8);

        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', '10'));
        $pdf->SetHeaderData(PDF_HEADER_LOGO, 60, 'SIWEBSU', 'Sistema Web de Gestión de Presupuestos SENESCYT-UNL');
        $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        $pdf->AliasNbPages();
        $pdf->AddPage();
        if ($this->si == true) {
            
            $pdf->writeHTML('<br><h3>PROYECTO:'.'  '.$proyectoss->getNombre().'</h3>');
            
            $pdf->writeHTML('<div align="center"><h2><b>ANÁLISIS PRESUPUESTARIO</b></h2></div>');
            
            $pdf->SetDrawColor(84, 15, 100, 3);
            $pdf->SetFillColor(84, 15, 100,34);
            $pdf->SetTextColor(255, 255, 255,255);
            
            $pdf->cell(75, 10, 'RUBROS', 1, 0, "C");
            $pdf->cell(40, 10, 'Valores' . ' ' . strtoupper($this->fond->getSiglasInst()), 1, 0, "C");
            $pdf->cell(60, 10, '% FINANCIAMIENTO' . ' ' . strtoupper($this->fond->getSiglasInst()), 1, 1, "C");
            $i = 0;
            $porcentajetotal1 = 0;
            $porcentajetotal2 = 0;
            $seguimiento = 0;
            $totalsenescyt = 0;
            
            $techos = Doctrine_Core::getTable('techos')->createQuery('t')
                        ->where('t.id_pro = ?', $this->idpro)
                        ->execute();
                foreach ($techos as $tp):
                    $idauxtp = $tp->getIdTp();
                    $sumavalor1 = 0;
                    $sumavalor2 = 0;
                    $descripciones = Doctrine_Core::getTable('descripciongeneral')->createQuery('d')
                            ->where('d.id_tp = ?', $tp->getIdTp())
                            ->execute();

                    foreach ($descripciones as $de):
                        $vals = Doctrine_Core::getTable('valor')->createQuery('v')
                                ->where('v.id_dg = ?', $de->getIdDg())
                                ->execute();

                        foreach ($vals as $va):                            
                                $sumavalor1 = $sumavalor1 + $va->getVal();                            
                        endforeach;
                    endforeach;


                    $porcentaje1 = 0;
                    $porcentaje1 = (100 * $sumavalor1) / $this->presupuesto;
                    $porcentajetotal1 = $porcentajetotal1 + $porcentaje1;
                    
                   $pdf->cell(75, 10, $tp->getNombre(), 1, 0, "C"); 
                   $pdf->cell(40, 10, round($sumavalor1,2), 1, 0, "C"); 
                   $pdf->cell(60, 10, round($porcentaje1,2), 1, 1, "C");
                   
              endforeach;
              
                  $pdf->cell(75, 10, 'MONTO DEL APORTE'.' '.strtoupper($this->fond->getSiglasInst()), 1, 0, "C");  
                  $pdf->cell(40, 10, round($this->presupuesto,2), 1, 0, "C"); 
                  $pdf->cell(60, 10, '  ', 1, 1, "C");
                  
                  $pdf->cell(75, 10, '% DE SEGUIMIENTO Y  MONITOREO  (  5% )', 1, 0, "C");
                  $pdf->cell(40, 10, round($seguimiento = $this->presupuesto * 0.05,2),1,0,"C");
                  $pdf->cell(60, 10,$seg=(($seguimiento/$this->presupuesto)*100).' '.'%',1,1,"C");
                  
                  $pdf->cell(75, 10, 'FINANCIAMIENTO'.' '.strtoupper($this->fond->getSiglasInst()), 1, 0, "C");
                  $pdf->cell(40, 10,round($totalsenescyt = $seguimiento + $this->presupuesto,2),1,0,"C");
                  $pdf->cell(60, 10,round($this->presupuestocontraparte,2),1,1,"C");
                  
                  $pdf->cell(75, 10, 'MONTO TOTAL DE EJECUCIÓN DEL PROYECTO', 1, 0, "C");
                  $pdf->cell(40, 10,round($seguimiento + $this->presupuesto,2),1,0,"C");
                  $pdf->cell(60, 10,round($porcentajetotal1 + $seg,2).' '.'%',1,0,"C");
                  $pdf->cell(40, 10,round($totalsenescyt,2),1,0,"C");
                  $pdf->cell(40, 10,'TOTAL PROYECTO',1,1,"C");
                  
            } else {
              $pdf->writeHTML('No se puede realizar el analisis presupuestario porque no existen valores ingresados en los correspondientes fondos</b>');  
            }      
                  
                  
       $proys=utf8_decode($proyectoss->getNombre());       
                    
        $pdf->ln();
        $pdf->Output('AnalisisPresupuestario_PROYECTO:_'.$proys.'.pdf', 'I');

        throw new sfStopException();
    }
    
    
    
    public function executeExportarAnalisisExcel(sfWebRequest $request) {

// Create new PHPExcel object

        $objPHPExcel = new sfPhpExcel();

// Set properties
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
        $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
        $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
        $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
        $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
        $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
        $objPHPExcel->getProperties()->setCategory("Test result file");


// Add some data
        $objPHPExcel->setActiveSheetIndex(0);
//$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello');
//$objPHPExcel->getActiveSheet()->setCellValue('B2', 'world!');
//$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Hello');
//$objPHPExcel->getActiveSheet()->setCellValue('D2', 'world!');
// Rename sheet
        $objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        $filename = 'AnalisisPresupuestario.xlsx';

        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Type: application/force-download');
        header('Content-Type: application/octet-stream');
        header('Content-Type: application/download');
        header("Content-Disposition: attachment;filename={$filename}");
        header('Content-Transfer-Encoding: binary');

// Save Excel 2007 file
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->setOffice2003Compatibility(true);
        $objWriter->save('php://output');

        $this->setLayout(false);

        throw new sfStopException();
    }

    public function executeAnalisis2(sfWebRequest $request) {
        $this->executeAnalisis($request);
    }
    public function executeAnalisis2L(sfWebRequest $request) {
        $this->executeAnalisis($request);
    }
    
    
    public function executeAnalisisFondo2(sfWebRequest $request) {
        $this->executeAnalisisFondo($request);
    }

    public function executeAnalisisFondo2L(sfWebRequest $request) {
        $this->executeAnalisisFondo($request);
    }

    public function executeconexion() {

        $this->servidor = "localhost";
        $this->usuario = "root";
        $this->clave = "";
        $this->basedatos = "sistemawebunlsenescyt";
        $this->conexion = mysql_connect($this->servidor, $this->usuario, $this->clave) or die(mysql_error());
        mysql_select_db($this->basedatos, $this->conexion) or die(mysql_error());
    }

    public function executetdanio() {

        $pdf = new sfTCPDF("L", "mm", "A4", true, 'UTF-8', false);
        $pdf->SetFont('FreeSerif', '', 9);
    }

    public function executePresupuestoFondo3(sfWebRequest $request) {
        $this->pro = $this->getUser()->getAttribute('id_prore');
        $idfo = $this->getUser()->getAttribute('id_fo');

        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idfo);
        $this->fondo = $f->fetchOne();
        $this->getUser()->setAttribute('id_fo', $idfo);
        $this->techospresupuestarioss = Doctrine_Core::getTable('techospresupuestarios')
                ->createQuery('a')
                ->execute();

        $partial->$this->getPartial('Reportes/_tabladinamica');

        $config = sfTCPDFPluginConfigHandler::loadConfig();
        $pdf = new sfTCPDF("L", "mm", "A4", true, 'UTF-8', false);
        $pdf->SetFont('FreeSerif', '', 7);
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', '13'));
        $pdf->SetHeaderData(PDF_HEADER_LOGO, 60, 'SIWEBSU', 'Sistema Web de Gestión de Presupuestos SENESCYT-UNL');
        $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        $pdf->AliasNbPages();
        $pdf->AddPage();

        $pdf->Output('doc.pdf', 'I');
        echo "<a href=doc.pdf>Descargar doc.pdf</a>";
        throw new sfStopException();
    }

    public function executeCronogramaFondoPDF(sfWebRequest $request) {

        $this->pro = $this->getUser()->getAttribute('id_prore');
        $idfo = $request->getParameter('id_fo');
        $this->getUser()->setAttribute('id_fo', $idfo);

        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idfo);
        $this->fondo = $f->fetchOne();
        $this->getUser()->setAttribute('id_fo', $idfo);

        $p = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro = ?', $this->pro);
        $this->proyecto = $p->fetchOne();


        $this->objetivos = Doctrine_Core::getTable('objetivo')->createQuery('o')
                ->where('o.id_pro = ?', $this->pro)
                ->execute();

        $this->techospresupuestarioss = Doctrine_Core::getTable('techospresupuestarios')
                ->createQuery('a')
                ->execute();


        $config = sfTCPDFPluginConfigHandler::loadConfig();
        $pdf = new sfTCPDF("L", "mm", "A4", true, 'UTF-8', false);
        $pdf->SetFont('FreeSerif', '', 7);
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', '13'));
        $pdf->SetHeaderData(PDF_HEADER_LOGO, 60, 'SIWEBSU', 'Sistema Web de Gestión de Presupuestos SENESCYT-UNL');
        $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);


        $meses = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
        $Recursos = array("Recursos Humanos", "Viajes Tecnicos", "Equipos", "Recursos Bibliograficos", "Materiales y Suministros", "Transferencia de Resultados", "Subcontratos y Servicios");


        $pdf->AliasNbPages();
        $pdf->AddPage();
        $pdf->Output('doc.pdf', 'I');
        echo "<a href=doc.pdf>Descargar doc.pdf</a>";
        throw new sfStopException();
    }

    public function executeCronogramaFondo3(sfWebRequest $request) {

        $this->pro = $this->getUser()->getAttribute('id_prore');
        $idfo = $this->getUser()->getAttribute('id_foaux');
        $this->getUser()->setAttribute('id_fo', $idfo);

        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idfo);
        $this->fondo = $f->fetchOne();

        $this->getUser()->setAttribute('id_fo', $idfo);

        $p = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro = ?', $this->pro);
        $this->proyecto = $p->fetchOne();


        $this->objetivos = Doctrine_Core::getTable('objetivo')->createQuery('o')
                ->where('o.id_pro = ?', $this->pro)
                ->execute();
    }

    public function executeCronogramaPDF(sfWebRequest $request) {

        $this->executePresupuestoFondo($request);
    }

}

